package com.meihuak.cloud_ordering.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meihuak.cloud_ordering.bean.PostTable;
import com.meihuak.cloud_ordering.mapper.PostTableMapper;
import com.meihuak.cloud_ordering.service.PostTableService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PostTableServiceImpl extends ServiceImpl<PostTableMapper, PostTable> implements PostTableService{
    /**
     * 删除帖子
     *
     * @param id 帖子id
     */
    @Override
    public void delPostTableById(int id) {
        this.removeById(id);

    }

    /**
     * 将map转化为posttable对象
     *
     * @param map
     */
    @Override
    public PostTable mapToPostTable(Map<String, Object> map) {
        return new PostTable(null,(String)map.get("title"),
                (String)map.get("content")
                ,(int)map.get("userid"),0,0,0,(String)map.get("ptype")
                ,new java.util.Date());
    }

    /**
     * 发布帖子
     *
     */
    @Override
    public void createPost(String title,String content,int userid,String ptype) {
        PostTable postTable = new PostTable(null,title,content,userid,
                0,0,0,ptype,new Date());
        this.save(postTable);
    }

    /**
     * 获取帖子,分页查询
     */
    @Override
    public List<PostTable> getAllPost(int i,int m,String ptype) {
        QueryWrapper<PostTable> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("ptype",ptype)
                .orderByDesc("create_date").last("limit "+i*m+","+m);//从i下标开始获取m条数据
        return this.list(queryWrapper);
    }
}
